/* PexifTool - $Revision$
 * Copyright (C) 2011 Petros Dolaschjan.  All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 *  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
 *  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *	$Id$
 */
package de.pd.pjim2.util;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import de.pd.pjim2.bean.PJSettings;

/**
 * @author petros
 * 
 */
public class FileUtil {
	

	/**
	 * 
	 */
	private static final String SEQ_PREFIX = "_seq";

	/**
	 * Returns the file extension.
	 * 
	 * @param file
	 * @return
	 */
	public static String getFileExtension(String file) {
		int lastDot = file.lastIndexOf(".");
		String ext = file.substring(lastDot + 1, file.length());
		return ext;
	}
	
	public static String getNextFilenameSequence(String file) {
		if(file == null) return null;
		int lastIndexOf = file.lastIndexOf(SEQ_PREFIX);
		String fname = file;
		String extWithDot = "." + getFileExtension(file);
		
		
		
		if(lastIndexOf != -1) {
			String seq_nr = file.substring(lastIndexOf + SEQ_PREFIX.length(), file.lastIndexOf(extWithDot));
			int next_seq = -1;
			try {
				next_seq = Integer.parseInt(seq_nr) + 1;
			} catch(Exception ex) {
				System.out.println("Error parsing sequence number " + seq_nr);
				return null;
			}
			String next_seq_str = (next_seq < 10) ? "0" + next_seq : "" + next_seq;
			fname = file.substring(0, lastIndexOf) + SEQ_PREFIX + next_seq_str + "." + getFileExtension(file);
		} else {
			fname = file.substring(0, file.lastIndexOf(extWithDot)) + SEQ_PREFIX + "01." + getFileExtension(file);
		}
		
		return fname;
	}

	/**
	 * Returns the last directory of a full path. e.g. /tmp/sub1/sub2 ->
	 * /tmp/sub1
	 * 
	 * @param dir
	 * @return
	 */
	public static String getLastDir(String dir) {
		String lastDir = dir.substring(dir.lastIndexOf(File.separator) + 1);
		return lastDir;
	}

	/**
	 * Checks if the given dir is already in a date format used by
	 * PJImageManager.
	 * 
	 * @param dir
	 *            The diretory to check.
	 * @return true, if its already in pjim format.
	 */
	public static boolean isDirDateFormat(String dir) {
		DateFormat format = new SimpleDateFormat("yyy_MM_dd");
		try {
			format.parse(dir);
		} catch (ParseException e) {
			return false;
		}
		return true;
	}

	/**
	 * Open a folder using the OS's default command.
	 * @throws IOException 
	 * 
	 */
	public static void openFolder(File pDir) throws IOException {
		if (Desktop.isDesktopSupported()) {
			Desktop.getDesktop().open(pDir);
		}

	}
	
	public static void main(String[] args) {
		System.out.println(FileUtil.getNextFilenameSequence("img_2011_10_01__17_15_07_seq03.JPG"));
	}

	/** Returns the converted file name using the settings and the date.
	 * @param date
	 * @param ext
	 * @return
	 */
	public static String dateToFilename(Date date, PJSettings pSettings, String ext) {
		DateFormat format;
		try {
			format = new SimpleDateFormat(pSettings.getFilenamePattern());
		} catch(Exception ex) {
			return null;
		}

		String dateFormat = format.format(date);
		dateFormat = dateFormat.replaceFirst("__24_", "__00_"); // replace hour
																// 24 with 00
		String fileName = pSettings.getImgPrefix()
				+ dateFormat + pSettings.getImgSuffix() + "." + ext;
		return fileName;
	}
	
	/** Returns the converted date name using the settings and the date.
	 * @param date
	 * @return
	 */
	public static String dateToFoldername(Date date, PJSettings pSettings) {
		DateFormat format;
		try {
			format = new SimpleDateFormat(pSettings.getDirnamePattern());
		} catch(Exception ex) {
			return null;
		}
		String dirName = format.format(date);
		return dirName;
	}
}
